package com.hup.minicontroller.server.view;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.hup.minicontroller.common.constant.NetConstant.ConnectType;
import com.hup.minicontroller.common.view.BaseAdapter;
import com.hup.minicontroller.common.view.BaseViewHolder;
import com.hup.minicontroller.server.R;
import com.hup.minicontroller.server.model.OnlineClientSession;
import com.hup.minicontroller.server.view.ClientItemAdapter.MyViewHolder;

import butterknife.BindView;

public class ClientItemAdapter extends BaseAdapter<OnlineClientSession, MyViewHolder> {
    private static final String TAG = ClientItemAdapter.class.getSimpleName();

    @Override
    public int getLayoutResId() {
        return R.layout.item_client;
    }

    @Override
    public MyViewHolder createHolder() {
        return new MyViewHolder();
    }

    public static class MyViewHolder implements BaseViewHolder<OnlineClientSession>, OnClickListener {
        @BindView(R.id.txt1)
        TextView txt1;
        @BindView(R.id.txt2)
        TextView txt2;
        @BindView(R.id.btnLogout)
        Button btnLogout;

        private OnlineClientSession session;

        @Override
        public void onBindView(View view) {
            btnLogout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
//            Log.i(TAG, "onClick: ");
            if (session.getConnectType() == ConnectType.LAN) {
                session.getChannel().close();
            } else {
                session.getBtChannel().disconnect();
            }
        }

        @Override
        public void bindData(int position, OnlineClientSession item) {
//            Log.i(TAG, "bindData: position={}" + position + ", item={}" + item);
            session = item;
            txt1.setText(String.format("%s\t %s", item.getId(), item.getLoginTimeStr()));
            if (item.getConnectType() == ConnectType.LAN) {
                txt2.setText(String.format("%s\t %s", item.getConnectType().desc, item.getIp()));
            } else {
                txt2.setText(String.format("%s\t", item.getConnectType().desc));
            }
        }
    }

}
